package com.mwx.baselib.net;

import android.app.Activity;
import android.content.Context;
import android.net.ParseException;
import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.mwx.baselib.constant.Constant;
import com.mwx.baselib.utils.MLog;
import com.mwx.baselib.utils.ToastUtils;
import com.mwx.baselib.view.LoadingDialog;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class DefaultObserver<T extends BaseResponses> implements Observer<T> {

    private Context activity;
    //  Activity 是否在执行onStop()时取消订阅
    private boolean isAddInStop = false;
    private LoadingDialog dialogUtils;

    public DefaultObserver(Context activity) {
        this.activity = activity;
        dialogUtils= LoadingDialog.getLoading(activity);
        dialogUtils.show();
    }
    public DefaultObserver(Context activity, boolean isshowShortToastLoading) {
        this.activity = activity;
        dialogUtils= LoadingDialog.getLoading(activity);
        if (isshowShortToastLoading) {
            dialogUtils.show();
        }
    }
    public DefaultObserver(Context activity, boolean isshowShortToastLoading, String msg) {
        this.activity = activity;
        dialogUtils= LoadingDialog.getLoading(activity);
        if (isshowShortToastLoading) {
            dialogUtils.show(msg);
        }
    }
    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    @Override
    public void onNext(@NonNull T t) {
        dismissProgress();
        if (t.getCode()==200) {
            if (t.getData()==null||"".equals(t.getData())){
                onException(ExceptionReason.PARSE_ERROR);
            }else {
                onSuccess(t);
            }
        }else {
            onFail(t);
        }
    }

    private void dismissProgress(){
        if(dialogUtils!=null){
            dialogUtils.close();
        }
    }


    @Override
    public void onError(@NonNull Throwable e) {
        MLog.e(e.getMessage());
        dismissProgress();
        if (e instanceof HttpException) {     //   HTTP错误
            onException(ExceptionReason.BAD_NETWORK);
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {   //   连接错误
            onException(ExceptionReason.CONNECT_ERROR);
        } else if (e instanceof InterruptedIOException) {   //  连接超时
            onException(ExceptionReason.CONNECT_TIMEOUT);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {   //  解析错误
            onException(ExceptionReason.PARSE_ERROR);
        } else {
            onException(ExceptionReason.UNKNOWN_ERROR);
        }
    }

    @Override
    public void onComplete() {

    }

    /**
     * 请求成功
     *
     */
    abstract public void onSuccess(T t);

    /**
     * 服务器返回数据，但响应码不为200
     *
     * @param response 服务器返回的数据
     */
    public void onFail(BaseResponses response) {
        String message = response.getMsg();
        if (TextUtils.isEmpty(message)) {
            ToastUtils.showShortToast(activity,"服务器异常");
        } else {
            ToastUtils.showShortToast(activity.getApplicationContext(),message);
        }
    }

    /**
     * 请求异常
     *
     * @param reason
     */
    public void onException(ExceptionReason reason) {
        switch (reason) {
            case CONNECT_ERROR:
                ToastUtils.showShortToast(activity,"连接错误");
                break;

            case CONNECT_TIMEOUT:
                ToastUtils.showShortToast(activity.getApplicationContext(),"连接超时");
                break;

            case BAD_NETWORK:
                ToastUtils.showShortToast(activity.getApplicationContext(),"网络问题");
                break;
            case PARSE_ERROR:
                ToastUtils.showShortToast(activity.getApplicationContext(),"解析数据失败");
                break;

            case UNKNOWN_ERROR:
            default:
                ToastUtils.showShortToast(activity.getApplicationContext(),"服务器异常");
                break;
        }
    }

    /**
     * 请求网络失败原因
     */
    public enum ExceptionReason {
        /**
         * 解析数据失败
         */
        PARSE_ERROR,
        /**
         * 网络问题
         */
        BAD_NETWORK,
        /**
         * 连接错误
         */
        CONNECT_ERROR,
        /**
         * 连接超时
         */
        CONNECT_TIMEOUT,
        /**
         * 未知错误
         */
        UNKNOWN_ERROR,
    }
}

